//
//  MusicLBTDetailViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "MusicLBTDetailViewController.h"
#import "DataAnalysis.h"
#import "MusicLBTDetailTableViewCell.h"
#import "MusicSongList.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "MusicPlayViewController.h"

@interface MusicLBTDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain) UIView *titleView;

@property(nonatomic, retain) UITableView *tableView;

@property(nonatomic, retain) NSMutableArray *musicListArray;

@property(nonatomic, retain) NSDictionary *musicDic;

@property(nonatomic, retain) UIImageView *imageview;

@property(nonatomic, retain) UILabel *titleLabel;

@end

@implementation MusicLBTDetailViewController

- (void)dealloc
{
    [_value release];
    [_name release];
    [_titleView release];
    [_tableView release];
    [_musicListArray release];
    [_musicDic release];
    [_imageview release];
    [_titleLabel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.musicListArray = [NSMutableArray array];
    
    //解析数据
    [self loadData];
    
    //创建tableView
    [self createTableView];
    
    //创建标题
    [self createTitle];
}

#pragma mark 创建标题
-(void)createTitle
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,kHeight * 64)];
    [self.view addSubview:self.titleView];
    [self.titleView release];
    [_titleView release];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 150 * kWidth, self.titleView.frame.size.height / 2 - 10 * kHeight, kWidth * 300, kHeight * 30)];
    self.titleLabel.text = self.name;
    self.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.titleView addSubview:self.titleLabel];
    [_titleLabel release];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15 * kWidth, 25 * kHeight, 20 * kWidth, 20 * kHeight);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [self.titleView addSubview:backBtn];
    [backBtn addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)BackAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 解析数据
-(void)loadData
{
    NSString *idString = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@", self.value];
   [DataAnalysis dataAnalysisGETWithUrlString:[NSString stringWithFormat:@"%@?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.8825&longtitude=%@", idString, @"121.5396"] parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
//       NSLog(@"%@", responseObject);
//       NSLog(@"%@", [NSString stringWithFormat:@"%@?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2", idString]);
       self.musicDic = responseObject;
       for (NSDictionary *dic in [responseObject objectForKey:@"songs"]) {
           MusicSongList *musicList = [MusicSongList musicSongListWithDictionary:dic];
           [self.musicListArray addObject:musicList];
       }
       [self.tableView reloadData];
   } failureBlock:^(NSError *error) {
       NSLog(@"error: %@", error);
   }];
}

#pragma mark 创建tableView
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20 * kHeight, self.view.frame.size.width, self.view.frame.size.height + 20 * kHeight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    [_tableView release];
  
    [self.tableView registerClass:[MusicLBTDetailTableViewCell class] forCellReuseIdentifier:@"MusicLBTDetailTableViewCell"];
}

//返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musicListArray.count;
}

//返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * kHeight;
}

//返回重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicLBTDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicLBTDetailTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.musicSongList = self.musicListArray[indexPath.row];
    cell.row = indexPath.row;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

//点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicPlayViewController *player = [[MusicPlayViewController alloc]init];
    player.playListArray = self.musicListArray;
    player.backImage = [[self.musicDic objectForKey:@"image"] objectForKey:@"pic"];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:player animated:YES];
}

//头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 220)];
    [self.imageview sd_setImageWithURL:[NSURL URLWithString:[[self.musicDic objectForKey:@"image"] objectForKey:@"pic"]] placeholderImage:[UIImage imageNamed:@"placeHoder"]];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 120 * kHeight, self.view.frame.size.width, 40 * kHeight)];
    titleLabel.shadowColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:25 * kWidth];
    titleLabel.text = self.name;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.imageview addSubview:titleLabel];
    [titleLabel release];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30 * kWidth, titleLabel.frame.origin.y + 50 * kHeight, 20 * kWidth, 20 * kHeight)];
    image.image = [UIImage imageNamed:@"Music_lisentCount"];
    [self.imageview addSubview:image];
    [image release];
    
    UILabel *listenCount = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.origin.x + 25 * kWidth, image.frame.origin.y, 60 * kWidth, 20 * kHeight)];
    listenCount.text = [NSString stringWithFormat:@"%ld.%ld万",[self.listenCount integerValue] / 10000, [self.listenCount integerValue] / 1000 % 10];
    listenCount.font = [UIFont systemFontOfSize:17 * kWidth];
    listenCount.shadowColor = [UIColor blackColor];
    listenCount.textColor = [UIColor whiteColor];
    [self.imageview addSubview:listenCount];
    [listenCount release];
    
    UILabel *subTitle = [[UILabel alloc]initWithFrame:CGRectMake(listenCount.frame.origin.x + 55 * kWidth, image.frame.origin.y, self.view.frame.size.width - listenCount.frame.origin.x - 40 * kWidth, 20 * kHeight)];
    subTitle.font = [UIFont systemFontOfSize:17 * kWidth];
    subTitle.shadowColor = [UIColor blackColor];
    subTitle.textColor = [UIColor whiteColor];
    subTitle.text = [self.musicDic objectForKey:@"desc"];
    [self.imageview addSubview:subTitle];
    [subTitle release];
    
    return [self.imageview autorelease];
}

//返回头视图高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 220 * kHeight;
}

//返回尾视图高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1 * kHeight;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > -45 && scrollView.contentOffset.y < 800) {
        self.titleView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:scrollView.contentOffset.y / 800];
        self.titleLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:scrollView.contentOffset.y];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
